from __future__ import absolute_import
from forest.resource import Resource, ValidationError

class CreateUsers(Resource):
    '''
        Example::

            resource:
            - $: users
              name: user
              uid: 10000

        .. describe:: name

            Required. Name of the new user.


        .. describe:: uid

            Required. The UID for the new user.
            If none is given, the next available one is used.
            
        .. describe:: algo

            The password algorithm to use in case isCrypted=True.
            If none is given, the cryptPassword default is used.

        .. describe:: gecos

            The GECOS information (full name, office, phone, etc.).
            Defaults to "".

        .. describe:: groups

            A list of existing group names the user should be
            added to.  Defaults to [].

        .. describe:: homedir

            The home directory for the new user.  Defaults to
            /home/<name>.

        .. describe:: isCrypted

            Is the password kwargs already encrypted?  Defaults to False.

        .. describe:: lock

            Is the new account locked by default?  Defaults to False.

        .. describe:: password

            The password.  See isCrypted for how this is interpreted.
            Defaluts to None.

        .. describe:: root

            The directory of the system to create the new user
            in.  homedir will be interpreted relative to this.
            Defaults to /mnt/sysimage.

        .. describe:: shell

            The shell for the new user.  If none is given,
            the libuser default is used.


    '''
    __register__ = "users"

    def get_id(self):
        return ' '.join(['users:', self.get_name()])

    def get_name(self):
        return self.value.get('name')

    def get_algo(self):
        return self.value.get('algo', None) #hm

    def get_gecos(self):
        return self.value.get('gecos', '')

    def get_groups(self):
        return self.value.get('groups', [])

    def get_homedir(self):
        return self.value.get('homedir', '/home/{}'.format(self.get_name()))

    def get_isCrypted(self):
        return self.value.get('isCrypted', False)

    def get_lock(self):
        return self.value.get('lock', False)

    def get_password(self):
        return self.value.get('password', None)

    def get_root(self):
        return self.value.get('root', '/mnt/sysimage')

    def get_shell(self):
        return self.value.get('shell', None) #hm

    def get_uid(self):
        return self.value.get('uid', None) #hm

    def validate(self):

        valid_keys = ['name', 'algo', 'gecos', 'groups', 'homedir', 'isCrypted'
                     , 'lock', 'password', 'root', 'shell', 'uid']

        for k in self.value.iterkeys():
            if k in valid_keys:
                continue
            raise ValidationError("Undefined create user option {}".format(k))

        if 'name' not in self.value:
            raise ValidationError("Undefined user name. Please set it")


class CreateGroups(Resource):
    '''
        An example::

            resources:
              - $: groups
                name: group
                gid: 10000

        .. describe:: name

            Required. The group name. 
            
        .. describe:: gid

            Required. The group id number.

    '''
    __register__ = "groups"

    def get_id(self):
        return ' '.join(['groups:', self.get_name()])

    def get_name(self):
        return self.value.get('name')

    def get_gid(self):
        return self.value.get('gid')

    def validate(self):

        valid_keys = ['name', 'gid']

        for k in self.value.iterkeys():
            if k in valid_keys:
                continue
            raise ValidationError("Undefined create group option {}".format(k))

        if 'name' not in self.value:
            raise ValidationError('Missing  group name. Please set it')
